import { createRouter, createWebHashHistory } from 'vue-router'
import IndexView from '../views/index/IndexView.vue'
import LoginView from '../views/login/LoginView.vue'
import KindView from '@/views/kind/KindView.vue'
import HomeView from '@/views/Home.vue'
import store from '@/store'
import BookshelfView from '@/views/bookshelf/BookshelfView.vue'
import UserCenterView from '@/views/usercenter/UserCenterView.vue'
import KindDetailView from '@/views/kind/KindDetailView.vue'
import NovelDetailView from '@/views/bookDetail/NovelDetailView.vue'
import NovelReading from '@/views/bookDetail/NovelReading.vue'
import SearchBookView from '@/views/search/SearchBookView.vue'
import UserInfoView from '@/views/usercenter/UserInfoView.vue'
import MyCommentsView from '@/views/usercenter/MyCommentsView.vue'
import MyFansView from '@/views/usercenter/MyFansView.vue'
import MyFollowsView from '@/views/usercenter/MyFollowsView.vue'
import UserCenterEditView from '@/views/usercenter/UserCenterEditView.vue'
import RegisterView from '@/views/login/RegisterView.vue'

const routes = [
  {
    path: '/',
    redirect: '/home/index',
  },
  {
    path: '/index',
    redirect: '/home/index'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { title: '登录' },
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: { title: '注册' }
  },
  {
    path: '/home',
    redirect: '/home/index',
    component: HomeView,
    children: [
      {
        path: 'index',
        name: 'index',
        component: IndexView,
        meta: { title: '首页' }
      },
      {
        path: 'search',
        name: 'search',
        component: SearchBookView,
        query: {
          keyword: ''
        },
        meta: { title: '搜索' }
      },
      {
        path: 'kind',
        name: 'kind',
        component: KindView,
        meta: { title: '小说分类' },
        children: [
          {
            path: 'kindDetail/:id',
            name: 'KindDetail',
            component: KindDetailView,
            props: true
          }
        ]
      },
      {
        path: 'bookshelf',
        name: 'bookshelf',
        component: BookshelfView,
        meta: { title: '我的书架' }
      },
      {
        path: 'userCenter/:id',
        name: 'userCenter',
        component: UserCenterView,
        //redirect: '/home/userCenter/id/myComments',
        meta: { title: '个人中心' },
        children: [{
          path: 'info',
          name: 'userInfo',
          component:UserInfoView
        },{
          path: 'myComments',
          name: 'myComments',
          component:MyCommentsView
        },
        {
          path: 'myFans',
          name: 'myFans',
          component:MyFansView
        }, {
          path: 'myFollows',
          name: 'myFollows',
          component:MyFollowsView
        },]
      },
      {
        path: '/home/userCenter/edit',
        name: 'userCenterEdit',
        component: UserCenterEditView,
        props: true,
      },
      {
        path: '/home/detail/:id',
        name: 'novelDetail',
        component: NovelDetailView,
        props: true,
      }
    ]
  },
  {
    path: '/novelReading/:novelId/:chapterId',
    name: 'novelReading',
    component: NovelReading
  }

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 禁止游客访问的路径
const authUrl = ['/bookshelf', '/usercenter']

router.beforeEach((to, from, next) => {
  // 1. to 往哪里去， 到哪去的路由信息对象
  // 2. from 从哪里来， 从哪来的路由信息对象
  // 3. next() 是否放行
  // 如果next()调用，就是放行
  // next(路径) 拦截到某个路径页面
  const token = store.state.user.userInfo.token
  // console.log('token:', token)
  document.title = to.meta.title || '默认标题';
  if (!authUrl.includes(to.path)) {
    next()
    return
  }

  if (token) {
    next()
  // console.log('11111')
  } else {
  // console.log('22222')
    next('/login')
  }
})

export default router
